/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.app.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yanlight.app.dal.dao.GroupRoleDAO;
import com.yanlight.app.dal.entity.GroupRole;
import com.yanlight.app.response.GroupRoleDetailResponse;
import com.yanlight.app.service.GroupRoleService;

/**
 * 
 * @author haolw
 * @time 2017/07/18
 */
@Service
public class GroupRoleServiceImpl implements GroupRoleService {
	@Autowired
	private GroupRoleDAO groupRoleDAO;

	@Override
	public List<Integer> findRolesById(Integer roleId) {
		List<Integer> roles = new ArrayList<Integer>(8);
		GroupRole groupRole = groupRoleDAO.findById(roleId);
		if (groupRole != null) {
			Integer extId = groupRole.getExtId();
			if (extId != null) {
				GroupRole extGroupRole = groupRoleDAO.findById(extId);
				List<Integer> extroles = extGroupRole.getRoleList();
				if (extroles != null && !extroles.isEmpty()) {
					roles.addAll(extroles);
				}
			}
			List<Integer> roleList = groupRole.getRoleList();
			if (roleList != null && !roleList.isEmpty()) {
				roles.addAll(roleList);
			}
		}
		return roles;
	}

	@Override
	public GroupRoleDetailResponse findDetail(Integer roleId) {
		GroupRoleDetailResponse response = new GroupRoleDetailResponse();
		GroupRole groupRole = groupRoleDAO.findById(roleId);
		List<Integer> roles = findRolesById(roleId);
		BeanUtils.copyProperties(groupRole, response);
		response.setRoles(roles);
		return response;
	}
}
